import sys

def load_charset(charset_file):
    with open(charset_file, "r", encoding="utf-16-le") as file:
        charset = file.read().strip()
    return set(charset)

def check_document(document, charset):
    with open(document, "r", encoding="utf-16-le") as file:
        content = file.read()
        invalid_chars = []
        for char in content:
            if char not in charset and char not in (" ", "\n", "\r"):
                invalid_chars.append(char)

    return invalid_chars

def main():
    if len(sys.argv) != 2:
        print("Usage: python find.py <document_A>")
        return

    charset_file = "标准SJIS.tbl"  # 替换成你的码表文档名
    document_file = sys.argv[1]  # 获取命令行参数中的文档A的文件名

    charset = load_charset(charset_file)
    invalid_chars = check_document(document_file, charset)

    if not invalid_chars:
        print("all perfect")
    else:
        print("Invalid characters:")
        for char in invalid_chars:
            print(f"'{char}' is not in the charset. Code: {ord(char)}")

if __name__ == "__main__":
    main()
